package com.link.base.base.stocksellstorage.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.invloc.model.Invloc;
import com.link.base.base.invloc.service.InvlocService;
import com.link.base.base.stocksellstorage.model.Stocktaking;
import com.link.base.base.stocksellstorage.model.StocktakingItem;
import com.link.base.base.stocksellstorage.service.StocktakingItemService;
import com.link.base.base.stocksellstorage.service.StocktakingService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 */
@Controller
@RequestMapping("/link/Stocktaking")
public class StocktakingController extends BasicController<Stocktaking> {

    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Resource
    private StocktakingService stocktakingService;
    @Resource
    private StocktakingItemService stocktakingItemService;

    @Resource
    private InvlocService invlocService;

    @Override
    public BasicService<Stocktaking> getBasicService() throws Exception {
        return stocktakingService;
    }

    /**提交盘点*/
    @RequestMapping(value = "/commit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> commit(@RequestBody Stocktaking record, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            stocktakingService.commit(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    /**插入或者更新盘点*/
    @RequestMapping(value = "/upsert", method = RequestMethod.POST)
    @ResponseBody
    @Override
    public Map<String, Object> upsert(@RequestBody Stocktaking record, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (null == record.getId()) {
                record.setId(keyGenerateDao.keyGenerate());
            }
            stocktakingService.upsert(record);
            Stocktaking newRecord = stocktakingService.queryById(record);
            result.put("result", newRecord);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }


    /**
     * 插入头行
     *
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/upsertHeadAndLine", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> upsertHeadAndLine(@JsonParam Stocktaking entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Stocktaking stocktaking = stocktakingService.upsertHeadAndLine(entity);
            result.put("success", true);
            result.put("stocktaking", stocktaking);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 查询头行
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/queryHeadAndLineById", method = RequestMethod.POST)
    @ResponseBody
    Map<String, Object> queryHeadAndLineById(@RequestBody Stocktaking entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        Long id = entity.getId();
        try {
            if (null == id) {
                throw new ServiceException("PUBLIC-007");
            }
            Stocktaking stocktaking = stocktakingService.queryById(id);
            if (null != stocktaking) {
                StocktakingItem queryParam = new StocktakingItem();
                queryParam.setPageFlag(false);
                queryParam.setStocktakingId(entity.getId());
                //新建状态的盘点任务的
                String stockNew = "New";
                //新建的库存盘点需要拿到每一条商品的最新数量信息
                if (stockNew.equals(stocktaking.getStatus())) {
                    CoreUser user = UserUtil.getUser();
                    Invloc invlocParam = new Invloc();
                    invlocParam.setOwnerId(user.getAcctId());
                    List<Invloc> invlocList = invlocService.queryByAcctId(invlocParam);
                    queryParam.setInvlocId(invlocList.get(0).getId());
                }
                List<StocktakingItem> stocktakingItemList = stocktakingItemService.queryByExamplePage(queryParam);
                stocktaking.setStocktakingItemList(stocktakingItemList);
            }
            result.put("success", true);
            result.put("stocktaking", stocktaking);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
